/*
 * TMap Designer - A Thematic Map design software
 * Copyright (C) 2006 World Health Organization
 * http://www.who.int
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */
package who.tmap.ui.cd.point;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;

import javax.swing.BorderFactory;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;

import org.apache.log4j.Logger;

import who.tmap.bo.ThematicMap;
import who.tmap.bo.cd.CDPointSymbols;
import who.tmap.bo.cd.CDTypes;
import who.tmap.bo.cd.CartographicDisplay;
import who.tmap.bo.cd.impl.CartographicDisplayFactory;
import who.tmap.bo.cd.point.CDPoint;
import who.tmap.bo.cd.point.CDPointDataDrivenSize;
import who.tmap.common.TMapException;
import who.tmap.toolkit.shape.annotation.impl.ImageHelper;
import who.tmap.ui.cd.JComboBoxPattern;
import who.tmap.ui.cd.JPanelCDHelper;
import who.tmap.ui.util.JComboBoxColor;
import who.tmap.ui.util.MiscUIHelper;
import who.tmap.util.MiscHelper;

/**
 * @author stl
 *
 * To change the template for this generated type comment go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
public class JPanelCDPointDataDrivenSize extends JPanel implements PanelCDPoint {
	
    private static Logger logger = Logger.getLogger(JPanelCDPointDataDrivenSize.class); 	
		
	private JPanel thisPanel =this;
	private ThematicMap thematicMap =null;
	private boolean dirty =false;
	
	private JPanelCDHelper jPanelCDHelper =new JPanelCDHelper();	

	private JComboBox jComboBoxSymbol = null;
	private JComboBox jComboBoxFillPattern = null;
	private JComboBox jComboBoxColor = null;

	private JPanel jPanelInfo = null;

	private JComboBox jComboBoxSizeColumn = null;
	
	private ImageHelper imageHelper =new ImageHelper();
	private MiscUIHelper miscUIHelper =new MiscUIHelper();
	
	private boolean initializing;
	
	/**
	 * This is the default constructor
	 */
	public JPanelCDPointDataDrivenSize() {
		super();
		initialize();
	}
	
	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		initializing =true;
		
		this.setSize(300, 200);
		this.setLayout( new BorderLayout() );
		this.setBorder(BorderFactory.createEmptyBorder(5,0,0,0));		
		this.add(getJPanelInfo(), BorderLayout.NORTH);
		
		imageHelper.setImage( new MiscHelper().getImageIcon( DEFAULT_BITMAP ).getImage() );
		
		initializing =false;
	}
	
	/**
	 * This method initializes jPanelInfo	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanelInfo() {
		if (jPanelInfo == null) {
			GridLayout gridLayout = new GridLayout(4, 2, 5, 5);
			jPanelInfo = new JPanel();
			jPanelInfo.setLayout(gridLayout);
			
			jPanelInfo.add( new JLabel( "Symbol" ), null);
			jPanelInfo.add(getJComboBoxSymbol(), null);
			jPanelInfo.add( new JLabel( "Color" ), null);
			jPanelInfo.add(getJComboBoxColor(), null);			
			jPanelInfo.add( new JLabel( "Fill pattern" ), null);
			jPanelInfo.add(getJComboBoxFillPattern(), null);
			jPanelInfo.add( new JLabel( "Size column") , null);
			jPanelInfo.add(getJComboBoxSizeColumn(), null);
		}
		return jPanelInfo;
	}
	
	/**
	 * This method initializes jComboBoxShape
	 * 
	 * @return javax.swing.JComboBox
	 */
	private JComboBox getJComboBoxSymbol() {
		if(jComboBoxSymbol == null) {
			jComboBoxSymbol = new JComboBox( CDPointSymbols.names );
			
			jComboBoxSymbol.setSelectedIndex( CDPointSymbols.DEFAULT );
			
			jComboBoxSymbol.addActionListener( new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					setDirty( true );
					
					if ( jComboBoxSymbol.getSelectedIndex() == CDPointSymbols.BITMAP && !initializing ) {
						
						File file =miscUIHelper.openBitmapFile();
						if ( file != null ) {
					    	try {
								imageHelper.loadFile( file );
								setDirty( true );
							} catch (IOException e1) {
								logger.error( e1 );
							}
						}
						
					}
				}
			});
		}
		return jComboBoxSymbol;
	}
	
	/**
	 * This method initializes jComboBoxFillPattern
	 * 
	 * @return javax.swing.JComboBox
	 */
	private JComboBox getJComboBoxFillPattern() {
		if(jComboBoxFillPattern == null) {
			jComboBoxFillPattern = new JComboBoxPattern();
			jComboBoxFillPattern.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					setDirty( true );
				}
			});
		}
		return jComboBoxFillPattern;
	}
	
	/**
	 * This method initializes jComboBoxColor
	 * 
	 * @return javax.swing.JComboBox
	 */
	private javax.swing.JComboBox getJComboBoxColor() {
		if(jComboBoxColor == null) {
			jComboBoxColor = new JComboBoxColor();
			jComboBoxColor.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					setDirty( true );
				}
			});
		}
		return jComboBoxColor;
	}
	

	/**
	 * This method initializes jComboBoxScalingFactor	
	 * 	
	 * @return javax.swing.JComboBox	
	 */
	JComboBox getJComboBoxSizeColumn() {
		if (jComboBoxSizeColumn == null) {
			jComboBoxSizeColumn = new JComboBox();
		}
		return jComboBoxSizeColumn;
	}	
		
	public ThematicMap getThematicMap() {
		return thematicMap;
	}
	
	public void setThematicMap(ThematicMap thematicMap) {
		this.thematicMap = thematicMap;
	}
	
	public void refreshAttrColumnNames() {

	}
	
	public void refreshData() {
		if ( thematicMap.getCartographicDisplay() != null 
				&& thematicMap.getCartographicDisplay() instanceof CDPoint ) {
			CDPointDataDrivenSize cd =(CDPointDataDrivenSize)thematicMap.getCartographicDisplay();
			
			if ( cd != null ) {
				initializing =true;
				getJComboBoxSymbol().setSelectedItem( CDPointSymbols.names[ cd.getSymbol() ] );		
				if ( cd.getSymbol() == CDPointSymbols.BITMAP ) {
					imageHelper.setImage( cd.getImage() );
					imageHelper.setImageBytes( cd.getImageBytes() );
				}
				
				getJComboBoxSizeColumn().setSelectedItem( cd.getSizeColumn() );
				((JComboBoxColor)getJComboBoxColor()).setSelectedColor( cd.getColor() );
				getJComboBoxFillPattern().setSelectedIndex( cd.getFillPattern() );
				
				setDirty( cd.isDirty() );
				initializing =false;
			}
		}
	}	
	
	public CartographicDisplay getCartographicDisplay() throws TMapException {
		CDPointDataDrivenSize cd =(CDPointDataDrivenSize)CartographicDisplayFactory.getInstance().get( CDTypes.POINT_DATA_DRIVEN_SIZE );
		
		cd.setSymbol( getJComboBoxSymbol().getSelectedIndex() );
		
		if ( cd.getSymbol() == CDPointSymbols.BITMAP ) {
			try {
				cd.setImage( imageHelper.getImage() );
				cd.setImageBytes( imageHelper.getImageBytes() );
			} catch (Exception e) {
				logger.error( e );
			}
		}	
		
		cd.setColor( ((JComboBoxColor)getJComboBoxColor()).getSelectedColor() );
		cd.setPolygonAssociatedData( null );
		cd.setFillPattern( getJComboBoxFillPattern().getSelectedIndex() );
		cd.setSizeColumn( (String)getJComboBoxSizeColumn().getSelectedItem() );	
		
		cd.setDirty( isDirty() );
		
		return cd;
	}

	public boolean isDirty() {
		return dirty;
	}

	public void setDirty(boolean dirty) {
		this.dirty = dirty;
	}

		
}
